package com.wh1200.mybatis.xmlless.model

import com.wh1200.mybatis.xmlless.model.base.IExpressionToken

/**
 *
 * @author 吴昊
 * @date 2023/12/12 13:47
 * @since vv.0.0
 * @version 1.0
 */
class SqlExpression {

  private val nodes: MutableList<IExpressionToken> = mutableListOf()

  fun toSql(): String {
    return nodes.joinToString("") { it.toSql() }
  }

  override fun toString(): String {
    return toSql()
  }

  fun getNodes(): List<IExpressionToken> {
    return nodes.toList()
  }

  fun addAll(nodes: List<IExpressionToken>): SqlExpression {
    this.nodes.addAll(nodes)
    return this
  }

  fun replace(function: (IExpressionToken) -> IExpressionToken): SqlExpression {
    val list = nodes.toList()
    nodes.clear()
    addAll(list.map(function))
    return this
  }

}
